import 'dart:convert';
import 'package:jd_shopping/HttpRequest/SpUtils.dart';
class SearchServices {
  static setHistoryData(keywords) async {
    try {
      List searchListData = json.decode(await SpUtils.get('searchList'));
      print(searchListData);
      var hasData = searchListData.any((v) {
        return v == keywords;
      });
      if (!hasData) {
        searchListData.add(keywords);
        await SpUtils.save('searchList', json.encode(searchListData));
      }
    } catch (e) {

       if(keywords != null && keywords.toString().length != 0){
         List tempList = new List();
         tempList.add(keywords);
         await SpUtils.save('searchList', json.encode(tempList));

       }
      
    }
  }
  static getHistoryList() async{
     try {
      List searchListData = json.decode(await SpUtils.get('searchList'));
      return searchListData;
    } catch (e) {
      return [];
    }
  }

  static clearHistoryList(){    
       SpUtils.remove('searchList');
  }
  static removeHistoryData(keywords) async{    
      List searchListData = json.decode(await SpUtils.get('searchList'));
      searchListData.remove(keywords);
      await SpUtils.save('searchList', json.encode(searchListData));
  }

}
